from typing import Any

from recruit.adapter.database.models import User
from recruit.domain.exceptions.user import (
    UserExistsError,
    UserNotFoundException,
)
from recruit.domain.models.user import UserResult, UserCreate, UserUpdateDTO


class UserService:
    def check_user_exists(self, value: bool) -> None:
        if value:
            raise UserExistsError('Пользователь уже существует!')

    def check_user(self, value: bool) -> None:
        if not value:
            raise UserNotFoundException('Пользователь не найден!')

    def create_user(self, user: UserCreate) -> dict[str, Any,]:
        data = user.model_dump()
        data['user_id'] = str(data['user_id'])
        return data

    def update_user(self, user: UserUpdateDTO) -> dict[str, Any]:
        data = vars(user)
        return {
            key: value for key, value in data.items() if
            value is not None and key != 'id'
        }

    def get_user(self, user: User | None) -> UserResult:
        if user is None:
            raise UserNotFoundException('Пользователь не найден!')
        return UserResult(
            id=user.id,
            user_id=user.user_id,
            username=user.username
        )
